<template>
	<sc-dialog v-model="visible" :title="titleMap[mode]" show-fullscreen destroy-on-close width="750px" @close="close">
		<el-form ref="formRef" label-width="100px" :model="formData" :rules="rules">
			<el-form-item label="设备" prop="equipId">
				<EquipTableSelect
					v-model="equipmentSelected"
					placeholder="输选择设备"
					:disabled="mode == 'edit'"
					@change="change" />
			</el-form-item>
			<el-form-item label="点检项" prop="subjectCodes">
				<sc-form-table
					ref="table"
					v-model="formData.subjectCodes"
					:addTemplate="addSubjectTemplate"
					placeholder="暂无数据">
					<el-table-column prop="subjectCode" label="点检内容" width="420">
						<template #default="scope">
							<el-select v-model="scope.row.subjectCode" placeholder="请选择点检项">
								<el-option
									v-for="item in checkSubjectOptions"
									:key="item.id"
									:label="item.subjectName"
									:value="item.subjectCode" />
							</el-select>
						</template>
					</el-table-column>
				</sc-form-table>
			</el-form-item>
		</el-form>

		<template #footer>
			<el-button @click="close">取 消</el-button>
			<el-button :loading="isSaveing" type="primary" @click="save">确 定</el-button>
		</template>
	</sc-dialog>
</template>

<script>
import EquipTableSelect from "../../../components/EquipTableSelect"

export default {
	emits: ['complete'],
	components: {
		EquipTableSelect,
	},
	data() {
		return {
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
			},
			isSaveing: false,
			visible: false,
			checkSubjectOptions: [],
			equipmentSelected: {},
			addSubjectTemplate: {
				subjectCode: '',
			},
			formData: {
				id: 0,
				equipId: undefined,
				equipCode: '',
				equipName: '',
				subjectCodes: [],
			},
			rules: {
				equipId: [{ required: true, message: "请选择设备", trigger: "blur" }],
				subjectCodes: [{ required: true, message: "请输入点检项编号", trigger: "blur" }],
			},
		};
	},
	mounted() {
	},
	methods: {
		change(item) {
			this.formData.equipId = item.id;
			this.formData.equipCode = item.equipCode;
			this.formData.equipName = item.equipName;
		},
		async open(row) {
			if (!row) {
				this.mode = "add";
			} else {
				this.mode = "edit";
				let resp = await this.$API.equipchecktemplate.model.get(row.id);
				this.formData = resp.data;
				this.formData.subjectCodes = this.formData.subjectCodes.map(s => {
					return { subjectCode: s };
				});

				this.equipmentSelected.id = this.formData.equipId;
				this.equipmentSelected.equipCode = this.formData.equipCode;
				this.equipmentSelected.equipName = this.formData.equipName;
			}
			const resp2 = await this.$API.equipchecksubject.list.get();
			this.checkSubjectOptions = resp2.data;
			this.visible = true;
		},
		save() {
			this.$refs.formRef.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.formData.subjectCodes = this.formData.subjectCodes.map(s => s.subjectCode);
					let resp = null;
					if (this.formData.id === 0) {
						resp = await this.$API.equipchecktemplate.add.post(this.formData);
					} else {
						resp = await this.$API.equipchecktemplate.update.put(this.formData);
					}
					this.isSaveing = false;
					if (resp.code == 200) {
						this.$emit("complete");
						this.visible = false;
						this.$message.success("保存成功");
					} else {
						this.$alert(resp.message, "提示", { type: "error" });
					}
				}
			});
		},
		close() {
			this.formData = {
				id: 0,
				equipId: undefined,
				equipCode: '',
				equipName: '',
				subjectCodes: [],
			};
			this.$refs.formRef.resetFields();
			this.visible = false;
		},
	},
};
</script>
